<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="试卷ID" prop="paperId">
        <el-input
          v-model="queryParams.paperId"
          placeholder="请输入试卷ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试卷名称" prop="paperName">
        <el-input
          v-model="queryParams.paperName"
          placeholder="请输入试卷名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题目ID" prop="topicId">
        <el-input
          v-model="queryParams.topicId"
          placeholder="请输入题目ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题目名称" prop="topicName">
        <el-input
          v-model="queryParams.topicName"
          placeholder="请输入题目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="topicStatus">
        <el-select v-model="queryParams.topicStatus" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['course:paperTopic:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['course:paperTopic:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paperTopicList" @selection-change="handleSelectionChange">
      <el-table-column label="试卷ID" align="center" prop="paperId" />
      <el-table-column label="试卷名称" align="center" prop="paperName" />
      <el-table-column label="题目ID" align="center" prop="topicId" />
      <el-table-column label="题目名称" align="center" prop="topicName" :show-overflow-tooltip="true" />
      <el-table-column label="题目类型" align="center" prop="topicType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_topic_type" :value="scope.row.topicType"/>
        </template>
      </el-table-column>
      <el-table-column label="题目难度" align="center" prop="topicHard">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_topic_hard" :value="scope.row.topicHard"/>
        </template>
      </el-table-column>
      <el-table-column label="题目说明" align="center" prop="topicIntro" :show-overflow-tooltip="true" />
      <el-table-column label="题目答案" align="center" prop="topicAnswer" :show-overflow-tooltip="true" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="题目备注" align="center" prop="topicRemark" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="topicStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.topicStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['course:paperTopic:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['course:paperTopic:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改试卷与题目关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="试卷名称" prop="paperId">
          <el-select v-model="form.paperId"  filterable placeholder="请选择试卷">
          <el-option
            v-for="item in paperOption"
            :key="item.paperId"
            :value="item.paperId"
            :label="item.paperName"
            :disabled="item.paperStatus == 1"
          />
          </el-select>
        </el-form-item>
        <el-form-item label="题目名称" prop="topicId">
          <el-select v-model="form.topicId"  filterable placeholder="请选择试卷">
            <el-option
              v-for="topic in topicOption"
              :key="topic.topicId"
              :label="topic.topicName"
              :value="topic.topicId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPaperTopic, getPaperTopic, delPaperTopic, addPaperTopic, updatePaperTopic } from "@/api/course/paperTopic";
import { wholePaper } from "@/api/course/paper";
import { wholeTopic} from "@/api/course/topic";

export default {
  name: "PaperTopic",
  dicts: ['sys_normal_disable','sys_topic_hard','sys_course_source','sys_topic_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 试卷与题目关联表格数据
      paperTopicList: [],
      // 试卷数据
      paperOption: [],
      // 题目数据
      topicOption: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        topicId: null,
        topicName: null,
        paperId: null,
        paperName: null,
        topicType: null,
        topicHard: null,
        topicStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        paperId: [
          { required: true, message: "请选择试卷", trigger: "blur" }
        ],
        topicId: [
          { required: true, message: "请选择题目", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询试卷与题目关联列表 */
    getList() {
      this.loading = true;
      if(this.$route.params.paperId !=null){
        this.queryParams.paperId = this.$route.params.paperId;
        this.$route.params.paperId = null;
      }
      listPaperTopic(this.queryParams).then(response => {
        this.paperTopicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询试卷列表 */
    getWholePaper() {
      wholePaper(this.queryParams).then(response => {
        this.paperOption = response.data;
      });
    },
     /** 查询题目列表 */
    getWholeTopic() {
      wholeTopic(this.queryParams).then(response => {
        this.topicOption = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        paperId: null,
        topicId: null,
        sort: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.paperId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getWholePaper();
      this.getWholeTopic();
      this.open = true;
      this.title = "添加试卷题目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getWholePaper();
      this.getWholeTopic();
      const paperId = row.paperId || this.ids;
      const topicId = row.topicId;
      getPaperTopic(paperId,topicId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改试卷题目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addPaperTopic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const paperId = row.paperId;
      const topicId = row.topicId;
      this.$modal.confirm('是否确认删除试卷ID为"' + paperId + '"的数据项？').then(function() {
        return delPaperTopic(paperId,topicId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/paperTopic/export', {
        ...this.queryParams
      }, `paperTopic_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
